package com.heifeng.cloudshelf.mvp.programme.activity;

import android.content.Context;
import android.content.Intent;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseActivity;
import com.heifeng.cloudshelf.dao.Programme3DDetailBean;
import com.heifeng.cloudshelf.event.ImageClickEvent;
import com.heifeng.cloudshelf.mvp.model.DetailImageInfo;
import com.heifeng.cloudshelf.mvp.programme.contract.ProgrammeDetailActivityContract;
import com.heifeng.cloudshelf.mvp.programme.presenter.ProgrammeDetailActivityPresenter;
import com.heifeng.cloudshelf.mvp.web.activity.WebViewActivity;
import com.heifeng.cloudshelf.utils.RichTextUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *方案详情
 */
public class ProgrammeDetailActivity extends BaseActivity<ProgrammeDetailActivityPresenter> implements ProgrammeDetailActivityContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

//    @BindView(R.id.iv_main)
//    ImageView ivMain;

    @BindView(R.id.iv_code)
    ImageView iv_code;

    @BindView(R.id.tv_all_num)
    TextView tvAll;

    @BindView(R.id.tv_select)
    TextView tvSelect;

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.l_video)
    LinearLayout l_video;

    @BindView(R.id.l_panorama)
    LinearLayout l_panorama;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private int selectPosition = 0;
    private int type;
    private String url;
    private String vidoeUrl;
    private RichTextUtils instance;

    public static void open(Context context, Object object) {
        Log.e("detail", "obj:" + object);
        Intent intent = new Intent(context, ProgrammeDetailActivity.class);
        DetailImageInfo.ImageInfoBean dao = (DetailImageInfo.ImageInfoBean) object;
        intent.putExtra("type", dao.getType());
        if (0 == dao.getType()) {
            Programme3DDetailBean pro = (Programme3DDetailBean) dao.getObj();
            intent.putStringArrayListExtra("images", (ArrayList<String>) pro.getImages());
            intent.putExtra("id", pro.getId());
            intent.putExtra("url", pro.getUrl());
        } else {
            int id = (int) dao.getObj();
            intent.putExtra("id", id);
        }
        context.startActivity(intent);
//        IntentUtil.startActivity(context, ProgrammeDetailActivity.class);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_programme_detail;
    }

    @Override
    protected void initEventAndData() {
        EventBus.getDefault().register(this);
        instance = RichTextUtils.getInstance(this, this);

        type = getIntent().getIntExtra("type", -1);
        if (0 == type) {
            ArrayList<String> images = getIntent().getStringArrayListExtra("images");
            Log.e("detail", "images:" + images);
            url = getIntent().getStringExtra("url");
            String id = getIntent().getStringExtra("id");
            Log.e("detail", "url:" + url);
            mPresenter.setImagesList(images);
            mPresenter.getTableModel(id);
            mPresenter.init();
        } else {
            int id = getIntent().getIntExtra("id", 0);
            mPresenter.getProductionInfo(id);
        }
    }

    public String getUrl() {
        return url;
    }

    @Override
    public ViewPager getViewPager() {
        return viewPager;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        instance.destory();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public TextView getTvAllNum() {
        return tvAll;
    }

    @Override
    public TextView getTvSelect() {
        return tvSelect;
    }


    @Override
    public void setCodeImage(String url) {
        if (!TextUtils.isEmpty(url)) {
            iv_code.setVisibility(View.VISIBLE);
            Glide.with(this).load(url).apply(new RequestOptions().error(R.mipmap.ic_wxacode)).into(iv_code);
        } else {
            iv_code.setVisibility(View.GONE);
        }
    }

    @Override
    public void setViewImage(String url, String video) {
        this.url = url;
        this.vidoeUrl = video;
        if (TextUtils.isEmpty(url)) {
            l_panorama.setVisibility(View.GONE);
        } else {
            if (0 != type) {
                l_panorama.setVisibility(View.VISIBLE);
            }
        }


        if (TextUtils.isEmpty(video)) {
            l_video.setVisibility(View.GONE);
        } else {
            l_video.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setTitle(String title) {
        instance.setRichText(title, tv_title);
//        tv_title.setText(title);
    }

    @OnClick({R.id.iv_left, R.id.iv_right, R.id.l_back, R.id.l_panorama, R.id.l_video})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                recyclerView.scrollToPosition(getLeftScrollNum());
                break;
            case R.id.iv_right:
                recyclerView.scrollToPosition(getRightScrollNum());
                break;
            case R.id.l_back:
                finish();
                break;
            case R.id.l_panorama://全景
                if (!TextUtils.isEmpty(url)) {
                    WebViewActivity.open(this, url);
                }
                break;
            case R.id.l_video://视频
                if (!TextUtils.isEmpty(vidoeUrl)) {
                    VideoPlayerActivity.StartActivity(this, vidoeUrl);
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void LoadMoreEvent(ImageClickEvent event) {
//        Glide.with(mContext)
//                .load(event.getImageUrl())
//                .into(ivMain);

        tvSelect.setText("" + (event.getSelectPos() + 1));

        selectPosition = event.getSelectPos();

        viewPager.setCurrentItem(selectPosition);
    }

    private static int scrollnum = 7;//一次滑动的个数

    public int getLeftScrollNum() {
        if (mPresenter.getFirstItemPos() > 0) {
            if (mPresenter.getFirstItemPos() - scrollnum > 0)
                return mPresenter.getFirstItemPos() - scrollnum;
            else
                return 0;
        } else
            return 0;
    }

    public int getRightScrollNum() {
        if (mPresenter.getLastItemPos() < mPresenter.getList().size() - 1) {
            if (mPresenter.getLastItemPos() + scrollnum < mPresenter.getList().size() - 1) {
                return mPresenter.getLastItemPos() + scrollnum;
            } else {
                return mPresenter.getList().size() - 1;
            }
        } else
            return mPresenter.getList().size() - 1;
    }
}
